<?php

class Skjb_View_Helper_FormMultiTransfer extends Zend_View_Helper_FormElement
{
    public function formMultiTransfer($name, $values = null, $attribs = null, $options = null)
    {
        if ($values === null) {
            $values = array();
        }
        
        $selectedItems = (isset($values['selected']) ? $values['selected'] : array());
        $addValue = (isset($values['button_add']) ? $values['button_add'] : array());
        $removeValue = (isset($values['button_remove']) ? $values['button_remove'] : array());
        
        if ($attribs === null) {
            $attribs = array();
        }
        
        $leftAttribs = (isset($attribs['select_left']) ? $attribs['select_left'] : array());
        $rightAttribs = (isset($attribs['select_right']) ? $attribs['select_right'] : array());
        $addAttribs = (isset($attribs['button_add']) ? $attribs['button_add'] : array());
        $removeAttribs = (isset($attribs['button_remove']) ? $attribs['button_remove'] : array());
        
        $tableAttribs = (isset($attribs['table']) ? $attribs['table'] : array());
        $tdLeftAttribs = (isset($attribs['td_left']) ? $attribs['td_left'] : array());
        $tdMiddleAttribs = (isset($attribs['td_middle']) ? $attribs['td_middle'] : array());
        $tdRightAttribs = (isset($attribs['td_right']) ? $attribs['td_right'] : array());
        
        $leftAttribs['multiple'] = 'multiple';
        $rightAttribs['multiple'] = 'multiple';
        $rightAttribs['id'] = 'select2_' . $name;
        
        $formSelect = new Zend_View_Helper_FormSelect();
        $formSelect->setView($this->view);
        
        $formButton = new Zend_View_Helper_FormButton();
        $formButton->setView($this->view);
        
        $leftItems = array();
        $rightItems = array();
        
        foreach ($options as $key => $value) {
            if (!in_array($key, $selectedItems)) {
                $leftItems[$key] = $value;
            } else {
                $rightItems[$key] = $value;
            }
        }
        
        $leftSelect = $formSelect->formSelect('select1_' . $name, null, $leftAttribs, $leftItems);
        $rightSelect = $formSelect->formSelect($name, null, $rightAttribs, $rightItems);
        $addButton = $formButton->formButton('add_option_' . $name, $addValue, $addAttribs);
        $removeButton = $formButton->formButton('remove_option_' . $name, $removeValue, $removeAttribs);
        
        $xhtml = '<script type="text/javascript">' . "\n"
               . '$().ready(function() {' . "\n"
               . '    $(\'#add_option_' . $name . '\').click(function() {' . "\n"
               . '        $(\'#select1_' . $name . ' option:selected\').remove().appendTo(\'#select2_' . $name . '\');' . "\n"
               . '    });' . "\n"
               . '    $(\'#remove_option_' . $name . '\').click(function() {' . "\n"
               . '        $(\'#select2_' . $name . ' option:selected\').remove().appendTo(\'#select1_' . $name . '\');' . "\n"
               . '    });' . "\n"
               . '});' . "\n"
               . '$(\'form\').submit(function() {' . "\n"
               . '    $(\'#select2_' . $name . ' option\').each(function(i) {' . "\n"
               . '        $(this).attr(\'selected\', \'selected\');' . "\n"
               . '    });' . "\n"
               . '});' . "\n"
               . '</script>' . "\n"
               . '<table' . $this->_htmlAttribs($tableAttribs) . '>' . "\n"
               . '    <tr>' . "\n"
               . '        <td' . $this->_htmlAttribs($tdLeftAttribs) . '>' . "\n"
               . $leftSelect
               . '        </td>' . "\n"
               . '        <td' . $this->_htmlAttribs($tdMiddleAttribs) . '>' . "\n"
               . $addButton
               . $removeButton
               . '        </td>' . "\n"
               . '        <td' . $this->_htmlAttribs($tdRightAttribs) . '>' . "\n"
               . $rightSelect
               . '        </td>' . "\n"
               . '    </tr>' . "\n"
               . '</table>' . "\n"
        ;

        return $xhtml;
    }
}

?>
